﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Linq;
using System.Data.OracleClient;

public partial class Admin_Khachhang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HienThiDanhSach();
    }

    void HienThiDanhSach()
    {
        // get all  list users
        DataTable dtsanpham = DataConnection.GetDatatableByStoreProcedureNameNoParam("sp_getAllUser");

        GridView1.DataSource = dtsanpham;
        GridView1.DataBind();

        MultiView1.ActiveViewIndex = 0;

    }

    void HienThiKetQuaSua(string tendn )
    {
        
        OracleParameter[] thamso = new OracleParameter[]
        {
            new OracleParameter("p_tendn",tendn)
        };

        // get User by TenDN
        DataTable dtuser = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_getUserByTenDN", thamso);

        lbid.Text = dtuser.Rows[0]["TenDN"].ToString();
        txthoten.Text = dtuser.Rows[0]["TenUsers"].ToString();
        txtdiachi.Text = dtuser.Rows[0]["DiaChi"].ToString();
        txtsdt.Text = dtuser.Rows[0]["SDT"].ToString();
        txtemail.Text = dtuser.Rows[0]["Email"].ToString();
        DropDownList1.Text = dtuser.Rows[0]["Quyen"].ToString();

        MultiView1.ActiveViewIndex = 1;
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string tendn = GridView1.Rows[e.RowIndex].Cells[7].Text;
        string tendns = Convert.ToString(Session["TenDN"]);
        if (tendns == tendn)
        {
            Label1.Visible = true;
            Label1.Text = "<strong>Không thể xóa tài khoản quản trị đang đăng nhập</strong>";
            return;

        }
        
        OracleParameter[] thamSo = new OracleParameter[]{
            new OracleParameter("p_tendn",tendn)
        };

        // delete User by TenDN
        DataConnection.GetDatatableByStoreProcedureName("delete_UsersByTenDN", thamSo);
        HienThiDanhSach();
        Label1.Visible = false;
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string tendn = GridView1.SelectedRow.Cells[7].Text;
        this.HienThiKetQuaSua(tendn);


    }


    protected void btnupdate_Click(object sender, EventArgs e)
    {

        //sua lai
        string tendn = lbid.Text;
        string quyen = DropDownList1.SelectedValue;
        {
            
            OracleParameter[] thamso = new OracleParameter[]
            {
                
                new OracleParameter("p_quyen",quyen),
                new OracleParameter("p_tendn",tendn),
            };

            // update Users(Quyen) by TenDN
            DataConnection.GetDatatableByStoreProcedureName("update_UsersByTenDN", thamso);
            this.HienThiDanhSach();
        }
    }

    protected void btntmkiem_Click(object sender, EventArgs e)
    {
       
        string tendn = txttim.Text;
        OracleParameter[] thamso = new OracleParameter[]{
            new OracleParameter("p_user",tendn)
        };

        // Search User By TenDN
        DataTable dt = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_SearchUsersByTenDN",thamso);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        txttim.Text = "";
    }
    protected void btnback_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
    protected void btntrove_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
    protected void txtsdt_TextChanged(object sender, EventArgs e)
    {

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.HienThiDanhSach();
    }
}